<?php

/**
 * Ajax layer class
 * 
 * @package DeadTouch AAC
 * @version 0.0.1
 * @author Kofel <kofels@gmail.com>
 * @copyright 2008 (C) by Kofel
 * @license http://www.gnu.org/licenses/lgpl-3.0.txt GNU Lesser General Public License, Version 3
 * @todo future: :D
 */
/*class jQField
{
	public static function value( $element, $id )
	{
		$id = ($id) ? '#' . $id . ' ' : NULL;
		return 'C|$("' . $id . $element . '").val()';
	}
}

class jQEvent
{
	public static function ready()
	{
		return "$(document).ready(function() {\n{_REPLACE_}\n});\n";
	}
	
	public static function click( $element, $id = NULL )
	{
		$id = ($id) ? '#' . $id . ' ' : NULL;
		return "$('" . $id . $element . "').click(function() {\n{_REPLACE_}\n});\n";
	}
	
	public static function change( $element, $id = NULL )
	{
		$id = ($id) ? '#' . $id . ' ' : NULL;
		return "$('" . $id . $element . "').change(function() {\n{_REPLACE_}\n});\n";
	}
	
	public static function func( $name, $args = array() )
	{		
		return "function " . $name . "(" . implode(', ', $args) . ") {\n{_REPLACE_}\n});\n";
	}
}
*/
class ajaxLayer
{
	public function is()
	{
		if( $_GET['ajax'] == 'yes' )
		{
			return true;
		}
		
		return false;
	}
	
	public function output( $string )
	{
		die( $string );
	}
	
	public function load()
	{
		Kernel::layer('js')->addFile('js/ajax.js');
	}
	/*
	public function jquery( $Call = array(), $Event = false, $Reaction = NULL )
	{
		$Parameters = '';
		
		foreach( $Call as $Name => $Value )
		{
			if( $Name && $Value )
			{
				$P = explode('|', $Value );
				
				if( count($P) > 1 )
				{
					$Parameters .= '&' . $Name . '=\' + ' . $P[1] . ' + \'';
				}
				else
				{
					$Parameters .= '&' . $Name . '=' . $Value;
				}
			}
		}
		
		$qUrl = 'index.php?c=' . $Call[0] . '&a=' . $Call[1] . '&ajax=yes' . $Parameters;
		
		$Code = "$.ajax({\nurl: '" . $qUrl . "',\ntype: 'GET',\ntimeout: 1000,\nsuccess: function( output ){\n" . $Reaction . "\n}";
		
		if( !$Event )
		{
			Kernel::layer('js')->addCode( $Code );
		}
		else
		{
			Kernel::layer('js')->addCode( str_replace('{_REPLACE_}', $Code, $Event ) );
		}
		
		return true;
	
		$.ajax({
    url: 'document.xml',
    type: 'GET',
    dataType: 'xml',
    timeout: 1000,
    error: function(){
        alert('Error loading XML document');
    },
    success: function(xml){
        // do something with xml
    }
});ivate

	}*/
	
	public function query($h, $Controller, $Action, $JS, $Params)
	{
		$P = '';
		foreach( $Params as $n => $v )
		{
			$P .= '&'.$n.'='.$v;
		}
		
		$Url = 'index.php?c=' . $Controller . '&a=' . $Action . '&ajax=yes' . $P;
	//		if (r.status == 200) { };	
		$i++;
		$Code = '
var ' . $h . ';
function status' . $h . '()
{
	if (' . $h . '.readyState == 4) {
		if (' . $h . '.status == 200) {
			var output = ' . $h . '.responseText;
			' . $JS . '
		};
	};
}

function ' . $h . 'Ajax()
{
' . $h . ' = getXMLHttpRequest();
' . $h . '.open( \'GET\', \'' . $Url . '\', true );
' . $h . '.onreadystatechange = status' . $h . ';
' . $h . '.send(null);
}';

		Kernel::layer('js')->addCode( $Code );
		return true;
	}
	
	//Some usefull functions :)
	
	public function fieldValue( $name )
	{
		return '\' + document.getElementById(\'' . $name . '\').value + \'';
	}
	
	public function jsFunction( $call, $code, $toUrl = FALSE ) //Remember to call with an arguments of course if they exists :) and ;
	{
		if( file_exists( $code ) )
		{
			Kernel::layer('js')->addFile( $code );
		}
		else
		{
			Kernel::layer('js')->addCode( $code );
		}
		
		if( $toUrl )
		{
			return '\' + ' . str_replace( ';', '', $call ) . ' + \''; 
		}
		else
		{
			return $call;
		}
	}
}
?>